package s19.ch22.ex4;

import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Main {

	JFrame converterJFrame;
	JPanel converterJPanel;
	JTextField tempCelsius;
	JLabel celsiusLabel, fahrenheitLabel;
	JButton convertJButton;

	private final class ActionListenerImplementation implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			int tempFahr = (int) ((Double.parseDouble(tempCelsius.getText())) * 1.8 + 32);
			fahrenheitLabel.setText(tempFahr + " Fahrenheit");
		}
	}

	public Main() {

		converterJFrame = new JFrame("Convert Celsius to Fahrenheit");
		converterJFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		converterJFrame.setSize(new Dimension(120, 40));

		converterJPanel = new JPanel(new GridLayout(2, 2));

		addWidgetsJPanel();

		converterJFrame.getRootPane().setDefaultButton(convertJButton);

		converterJFrame.add(converterJPanel, BorderLayout.CENTER);

		converterJFrame.pack();
		converterJFrame.setVisible(true);
	}

	private void addWidgetsJPanel() {

		tempCelsius = new JTextField(2);
		celsiusLabel = new JLabel("Celsius", SwingConstants.LEFT);
		convertJButton = new JButton("Convert");
		fahrenheitLabel = new JLabel("Fahrenheit", SwingConstants.LEFT);

		convertJButton.addActionListener(new ActionListenerImplementation());

		converterJPanel.add(tempCelsius);
		converterJPanel.add(celsiusLabel);
		converterJPanel.add(convertJButton);
		converterJPanel.add(fahrenheitLabel);

		celsiusLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		fahrenheitLabel.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
	}

	private static void createAndShowGUI() {
		JFrame.setDefaultLookAndFeelDecorated(true);
		Main converter = new Main();
	}

	public static void main(String[] args) {
		createAndShowGUI();
	}
}